package com.fortune.controller;

import com.fortune.base.BaseController;
import com.fortune.common.redis.RedisClient;
import com.fortune.constants.RoleConstants;
import com.fortune.constants.redis.CacheKeys;
import com.fortune.dto.excution.DriverExcution;
import com.fortune.dto.excution.SalesManExcution;
import com.fortune.entity.PhoneAndPassword;
import com.fortune.enums.DriverEnum;
import com.fortune.enums.SalesManEnum;
import com.fortune.service.DriverService;
import com.fortune.util.TokenUtil;
import com.fortune.vo.driver.DriverPasswordDto;
import com.fortune.vo.driver.DriverPersonalInfo;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fortune.base.BaseAPIExcution;
import com.fortune.dto.in.driver.DriverLoginDto;
import com.fortune.vo.salesMan.LoginSalesManInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(value = "wechat/driver", description = "司机账号相关", tags = {"微信小程序（司机）：账号相关"})
@RestController
@RequestMapping("wechat/driver")
public class DriverController extends BaseController{
	@Autowired
	private RedisClient redisClient;

	@Autowired
	private DriverService driverService;

	@ApiOperation(value = "司机登录", nickname = "driverLogin", notes = "", tags={ "@郭阳" })
    @PostMapping(value = "login",
        produces = { MediaType.APPLICATION_JSON_VALUE },
        consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<LoginSalesManInfo> login(@Validated @RequestBody DriverLoginDto loginDto) {
		String phone = loginDto.getPhone();
		String password = loginDto.getPassword();
		String findPostByPhone = driverService.findPostByPhone(phone);
		if(!findPostByPhone.equals(RoleConstants.DRIVER_ID)){
			return new SalesManExcution<LoginSalesManInfo>(null, SalesManEnum.DRIVER_LOGIN_ERROR);
		}
		//验证手机号是否存在  PhoneAndPassword这里有厂商和经销商id
		PhoneAndPassword findPhoneAndPassword = driverService.findPhoneAndPassword(phone);
		if(findPhoneAndPassword == null){
			return new SalesManExcution<LoginSalesManInfo>(null, SalesManEnum.ACCOUNT_IS_NOT_EXIST);
		}
		//验证密码是否正确
		String passwords = findPhoneAndPassword.getPassword();
		if(!password.equals(passwords)){
			return new SalesManExcution<LoginSalesManInfo>(null, SalesManEnum.PASSWORD_ERROR);
		}
		//验证账号是否被禁用
		String status = findPhoneAndPassword.getStatus();
		if(status.equals("NO")){
			return new SalesManExcution<LoginSalesManInfo>(null, SalesManEnum.ACCOUNT_IS_DISABLED);
		}
		String mid = findPhoneAndPassword.getMid();
		String did = findPhoneAndPassword.getDid();
		String id = findPhoneAndPassword.getId();
		//获取随机唯一字符串
		String token = TokenUtil.genToken();
		String loginToken = id + "," + mid + "," + did + "," + phone;
		//key CacheKeys+随机字符串 value 手机号 存到redis里
		redisClient.save(loginToken, 60*24, CacheKeys.SALES_MAN_LOGIN_TOKEN, token);
		LoginSalesManInfo findLoginSalesManInfo = driverService.findLoginSalesManInfo(phone);
		findLoginSalesManInfo.setToken(token);
		return new SalesManExcution<LoginSalesManInfo>(findLoginSalesManInfo, SalesManEnum.LOGIN_SUCCESS);
	}

	@ApiOperation(value = "司机忘记密码", nickname = "driverResetPassword", notes = "", tags={ "@郭阳" })
	@PostMapping(value = "password/reset",
			produces = { MediaType.APPLICATION_JSON_VALUE },
			consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> login(@Validated @RequestBody DriverPasswordDto driverPasswordDto) {
		String userId = getUserId();
		String phone = redisClient.get(CacheKeys.SALES_MAN_LOGIN_TOKEN, userId);
		String codeInfor = "123456";
		String code = driverPasswordDto.getCode();
		if(!codeInfor.equals(code)){
			return new SalesManExcution<Void>(null, SalesManEnum.CODE_ERROR);
		}
		String password = driverPasswordDto.getPassword();
		int editPasswordByPhoneAndCode = driverService.editPasswordByPhoneAndCode(phone,password);
		if(editPasswordByPhoneAndCode < 1){
			return new SalesManExcution<Void>(null, SalesManEnum.EDIT_PASSWORD_ERROR);
		}
		return new SalesManExcution<Void>(null, SalesManEnum.EDIT_PASSWORD_SUCCESS);
	}

	@ApiOperation(value = "司机个人信息", nickname = "driverPersonal", notes = "", tags={ "@郭阳" })
	@ApiImplicitParams({
			@ApiImplicitParam(name="Authorization", value="登录返回的token值",paramType="header",dataType="String",required = true)
	})
	@PostMapping(value = "password/personal",
			produces = { MediaType.APPLICATION_JSON_VALUE },
			consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<DriverPersonalInfo> personal() {
		String mid = getMId();
		String did = getDId();
		String userId = getUserId();
		/*String phone = redisClient.get(CacheKeys.SALES_MAN_LOGIN_TOKEN, userId);*/
		String phone = getPhone();
		DriverPersonalInfo driverPersonalInfo = new DriverPersonalInfo();
		driverPersonalInfo.setPhone(phone);
		DriverPersonalInfo findDriverPersonalInfo = driverService.findDriverPersonalInfo(mid, did, userId);
		String name = findDriverPersonalInfo.getName();
		driverPersonalInfo.setName(name);
		return new DriverExcution<DriverPersonalInfo>(driverPersonalInfo, DriverEnum.FIND_SUCCESS);
	}

	@ApiOperation(value = "司机修改姓名", nickname = "driverEditName", notes = "", tags={ "@郭阳" })
	@ApiImplicitParams({
			@ApiImplicitParam(name="name", value="司机姓名",paramType="query",dataType="string")
	})
	@PostMapping(value = "password/editName",
			produces = { MediaType.APPLICATION_JSON_VALUE },
			consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> editName(@Validated @NotBlank(message="司机姓名不能为空") String name) {
		String mid = getMId();
		String did = getDId();
		String userId = getUserId();
		int editDriverName = driverService.editDriverName(mid, did, userId, name);
		if(editDriverName < 1){
			return new DriverExcution<Void>(null, DriverEnum.EDIT_ERROR);
		}
		return new DriverExcution<Void>(null, DriverEnum.EDIT_SUCCESS);
	}

	@ApiOperation(value = "司机验证手机号", nickname = "driverVeriPhone", notes = "", tags={ "@郭阳" })
	@ApiImplicitParams({
			@ApiImplicitParam(name="phone", value="司机手机号",paramType="query",dataType="string"),
			@ApiImplicitParam(name="code", value="验证码",paramType="query",dataType="string"),
			@ApiImplicitParam(name="Authorization", value="登录返回的token值",paramType="header",dataType="String",required = true)
	})
	@PostMapping(value = "password/veriPhone",
			produces = { MediaType.APPLICATION_JSON_VALUE },
			consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> veriPhone(@Validated @NotBlank(message="司机手机号不能为空") String phone,
										   @Validated @NotBlank(message="验证码不能为空") String code) {
		String driverCode = "123456";
		if(!driverCode.equals(code)){
			return new DriverExcution<Void>(null, DriverEnum.CODE_ERROR);
		}
		return new DriverExcution<Void>(null, DriverEnum.CODE_SUCCESS);
	}

	@ApiOperation(value = "司机修改手机号", nickname = "driverEditPhone", notes = "", tags={ "@郭阳" })
	@ApiImplicitParams({
			@ApiImplicitParam(name="newPhone", value="司机新手机号",paramType="query",dataType="string"),
			@ApiImplicitParam(name="newCode", value="验证码",paramType="query",dataType="string"),
			@ApiImplicitParam(name="Authorization", value="登录返回的token值",paramType="header",dataType="String",required = true)
	})
	@PostMapping(value = "password/editPhone",
			produces = { MediaType.APPLICATION_JSON_VALUE },
			consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> editPhone(@Validated @NotBlank(message="司机新手机号") String newPhone,
										   @Validated @NotBlank(message="验证码不能为空") String newCode) {
		String mid = getMId();
		String did = getDId();
		String userId = getUserId();
		String driverCode = "1234";
		if(!driverCode.equals(newCode)){
			return new DriverExcution<Void>(null, DriverEnum.CODE_ERROR);
		}
		int editDriverPhone = driverService.editDriverPhone(mid, did, userId, newPhone);
		if(editDriverPhone < 1){
			return new DriverExcution<Void>(null, DriverEnum.EDIT_ERROR);
		}
		return new DriverExcution<Void>(null, DriverEnum.EDIT_SUCCESS);
	}
}
